package com.qdxwx.common.page;

import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;

import jakarta.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
public class PageRequest implements Serializable {

    private static final long serialVersionUID = 6489359158841475267L;

    private static final int defaultPageSize = 10;
    private long currentPage;
    private int pageSize;
    private long offset;
    //总记录数
    private long totalRecords;
    private String orderBy;

    public static PageRequest form(HttpServletRequest request) {
        int page = TKConvert.toInteger(request.getParameter("page"), 0);
        if (page <= 0) {
            page = TKConvert.toInteger(request.getParameter("pageNum"), 0);
        }
        int pageSize = TKConvert.toInteger(request.getParameter("limit"), 0);
        if (pageSize <= 0) {
            pageSize = TKConvert.toInteger(request.getParameter("pageSize"), 0);
            if (pageSize <= 0) {
                pageSize = 10;
            }
        }
        return new PageRequest(page, pageSize);
    }

    public PageRequest() {
        this(1);
    }

    public PageRequest(long currentPage) {
        this(currentPage, defaultPageSize);
    }

    public PageRequest(long currentPage, int pageSize) {
        if (currentPage < 1L) {
            currentPage = 1;
        }
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.offset = (this.currentPage - 1) * this.pageSize;
    }

    public int getPageSize() {
        return this.pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
        this.offset = (this.currentPage - 1) * this.pageSize;
    }

    public long getCurrentPage() {
        return this.currentPage;
    }

    public void setCurrentPage(long currentPage) {
        if (currentPage < 1) {
            currentPage = 1;
        }
        this.currentPage = currentPage;
        this.offset = (this.currentPage - 1) * this.pageSize;
    }

    public void setOffset(long offset) {
        this.offset = offset;
    }

    public long getOffset() {
        return this.offset;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(String orderBy) {
        if (TKString.isOrderBySql(orderBy)) {
            this.orderBy = orderBy;
        }
    }

    public long getTotalPages(long totalRecords) {
        this.totalRecords = totalRecords;
        return getTotalPages();
    }

    public long getTotalPages() {
        if (this.totalRecords < 1) return 0;
        long totalPages = this.totalRecords / (long) this.pageSize;
        if (this.totalRecords % (long) this.pageSize != 0L) {
            totalPages += 1;
        }
        return totalPages;
    }

    public long getTotalRecords() {
        return totalRecords;
    }

    public void setTotalRecords(long totalRecords) {
        this.totalRecords = totalRecords;
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        } else if (!(obj instanceof PageRequest)) {
            return false;
        } else {
            PageRequest other = (PageRequest) obj;
            return this.currentPage == other.currentPage && this.pageSize == other.pageSize;
        }
    }

    public int hashCode() {
        byte result = 1;
        int result1 = 31 * result + (int) this.currentPage;
        result1 = 31 * result1 + this.pageSize;
        return result1;
    }

    @Override
    public String toString() {
        return "PageRequest{" +
                "currentPage=" + currentPage +
                ", pageSize=" + pageSize +
                ", offset=" + offset +
                ", orderBy='" + orderBy + '\'' +
                '}';
    }
}
